package cn.edu.neu.vmmigeval.evaluation.loadevaluation;

import cn.edu.neu.vmmigeval.cloud.entity.Vm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class LoadCombination2 {
    //  组合算法

    public static void main(String[] args) {
    	LoadCombination2 s = new LoadCombination2();
        s.printLoadCombination();      
    }
    
    /**
     * 
     */
    public void printLoadCombination(){
        double[] num = new double[]{1,2,3,4,5};
        List<Vm> vms = new ArrayList<Vm>();
        Vm vm1 = new Vm();
        vm1.setId(1);
        vm1.setScore(0.5);
        Vm vm2 = new Vm();
        vm2.setId(2);
        vm2.setScore(0.6);
        Vm vm3 = new Vm();
        vm3.setId(3);
        vm3.setScore(0.7);
        Vm vm4 = new Vm();
        vm4.setId(4);
        vm4.setScore(0.8);
        vms.add(vm1);
        vms.add(vm2);
        vms.add(vm3);
        vms.add(vm4);
      //  print(combine(vms,3));
        Map<Integer, List<Double[]>> map = new HashMap<Integer,List<Double[]>>();
        for (int i = 1; i < vms.size(); i++) {
        	List<Double[]> list = combine(vms,i);
        	print(list);
        	System.out.println("--------------------");
			map.put(i, list);
		}
    }
    public  Map<Integer, List<Double[]>> getVmMap(List<Vm> vms){
    	 Map<Integer, List<Double[]>> map = new HashMap<Integer,List<Double[]>>();
         for (int i = 1; i < vms.size(); i++) {
        	 List<Double[]> list = combine(vms,i);
         	print(list);
         	System.out.println("--------------------");
 			map.put(i, list);
 		}
         return map;
    }
    /**
     * 从n个数字中选择m个数字
     * @param a
     * @param m
     * @return
     */
    public List<Double[]> combine(List<Vm> a,int m){
        //int n = a.length;
        int n = a.size();
        if(m>n){
            try {
				//throw new Exception("错误！数组a中只有"+n+"个元素。"+m+"大于"+n+"!!!");
				return null;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
        List result = new ArrayList();
        
        double[] bs = new double[n];
        for(int i=0;i<n;i++){
            bs[i]=0;
        }
        //初始化
        for(int i=0;i<m;i++){
            bs[i]=1;
        }
        boolean flag = true;
        boolean tempFlag = false;
        int pos = 0;
        int sum = 0;
        //首先找到第一个10组合，然后变成01，同时将左边所有的1移动到数组的最左边
        do{
            sum = 0;
            pos = 0;
            tempFlag = true; 
            result.add(print(bs,a,m));
            
            for(int i=0;i<n-1;i++){
                if(bs[i]==1 && bs[i+1]==0 ){
                    bs[i]=0;
                    bs[i+1]=1;
                    pos = i;
                    break;
                }
            }
            //将左边的1全部移动到数组的最左边
            
            for(int i=0;i<pos;i++){
                if(bs[i]==1){
                    sum++;
                }
            }
            for(int i=0;i<pos;i++){
                if(i<sum){
                    bs[i]=1;
                }else{
                    bs[i]=0;
                }
            }
            
            //检查是否所有的1都移动到了最右边
            for(int i= n-m;i<n;i++){
                if(bs[i]==0){
                    tempFlag = false;
                    break;
                }
            }
            if(tempFlag==false){
                flag = true;
            }else{
                flag = false;
            }
            
        }while(flag);
        result.add(print(bs,a,m));
        
        return result;
    }
    
    private double[] print(double[] bs,List<Vm> a,int m){
    	double[] result = new double[m];
        int pos= 0;
        for(int i=0;i<bs.length;i++){
            if(bs[i]==1){
                result[pos]=a.get(i).getId();//a.get(i).getScore();
                pos++;
            }
        }
        return result ;
    }
    
    private void print(List l){
        for(int i=0;i<l.size();i++){
            double[] a = (double[])l.get(i);
            for(int j=0;j<a.length;j++){
                System.out.print(a[j]+" ");
            }
            System.out.println();
        }
    }
}